package cn.bdqn.repository;

import cn.bdqn.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryText {
    @Resource
    UserRepository userRepository;

    @Test
    public void insert(){
        User user=new User("雷某","123456",1l,1);
        User save = userRepository.save(user);

    }
     @Test
    public void update(){
        User user=new User(21l,"雷某","123456",1l,1);
        User save = userRepository.save(user);
    }

    @Test
    public void delete(){
        userRepository.deleteById(18l);
    }

    @Test
    public void test5(){
        User user = userRepository.findById(18l).get();
        System.out.println(user);
    }

    @Test
    public void test6(){
        PageRequest pageRequest=PageRequest.of(0,2);
        Page<User> pa = userRepository.findAll(pageRequest);
        System.out.println("第几页："+(pa.getNumber()+1));
        System.out.println("每一页多少条："+pa.getSize());
        System.out.println("总共多少条记录："+pa.getTotalElements());
        System.out.println("总共多少页:"+pa.getTotalPages());
        System.out.println("每页的记录："+pa.getContent());

    }
    @Test
    public void test7(){
        List<User> users = userRepository.selectById(2l);
        for (User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public void test8(){
        List<User> lisi = userRepository.queryByUsrName("lisi");
        for (User user : lisi) {
            System.out.println(user);
        }
    }
    @Test
    public void test9(){
        List<User> lisi = userRepository.queryByUsrNameLike("%t%");
        for (User user : lisi) {
            System.out.println(user);
        }
    }
    @Test
    public void test10(){
        List<User> lisi = userRepository.queryByUsrNameOrUsrPassword("lisi", "123456");
        for (User user : lisi) {
            System.out.println(user);
        }
    }
    @Test
    public void test11(){
        Specification<User> specification=new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path usrName = root.get("usrName");
                Path usrPassword = root.get("usrPassword");

//                Predicate equal = criteriaBuilder.equal(usrRoleId, "2");
                Predicate like = criteriaBuilder.equal(usrName, "li");
                Predicate like2 = criteriaBuilder.equal(usrPassword, "123456");
//                Predicate and = criteriaBuilder.and(like, like2);
                Predicate and = criteriaBuilder.or(like, like2);
                return and;
            }
        };
        List<User> all = userRepository.findAll(specification);
        for (User user : all) {
            System.out.println(user);
        }

    }
}
